//
//  GHBaseWebViewController.swift
//  KYC2C
//
//  Created by Guanghui Liao on 12/20/17.
//  Copyright © 2017 liaoworking. All rights reserved.
//

import UIKit
import WebKit
import MJRefresh
import SnapKit
class GHBaseWebViewController: GHBaseViewController,WKUIDelegate,WKNavigationDelegate,WKScriptMessageHandler {
 
    var url:String = ""//请求链接
    
    var paraDict:Dictionary<String, String> = { ()-> Dictionary<String, String> in
        let paraDict = ["apps":"1"]
        return paraDict
        }()
    
    // 顶部刷新
    var refreshHeader = MJRefreshNormalHeader()
    
    var  webView:WKWebView = {()->WKWebView in
        let webView = WKWebView(frame: UIScreen.main.bounds)
        return webView
    }()
    
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        addAllScriptMsgHandle()
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewDidAppear(animated)
        removeAllScriptMsgHandle()
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        configWebView()
        initData()
        loadData()
    }

    func configWebView() {
        // 下拉刷新
        refreshHeader.setRefreshingTarget(self, refreshingAction: #selector(headerRefresh))
        webView.scrollView.mj_header = refreshHeader
        webView.uiDelegate = self
        webView.navigationDelegate = self
        view.addSubview(webView)
        webView.snp.makeConstraints { (make) in
            make.bottom.equalTo(self.view.snp.bottom)
            make.top.equalTo(self.view.snp.top)
            make.left.equalTo(self.view.snp.left)
            make.right.equalTo(self.view.snp.right)
        }
    }
    
    func initData() {
        self.url = "\(self.url)?"
        for (key , value) in paraDict
        {
            self.url.append("&\(key)=\(value)")
        }
    }
    
    func loadData() {
        let request = NSURLRequest(url: URL(string: url)!)
        webView.load(request as URLRequest)
        print("请求链接-\(request)")
//        showRequestHUD(nil, self.view);
    }
    
   @objc func headerRefresh() {
    webView.reload()
    webView.scrollView.mj_header.endRefreshing()
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}


extension GHBaseWebViewController {
   
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        self.title = webView.title
        //TODO: to add hud
//        hidenRequestHUD()
    }
    
    
    func addAllScriptMsgHandle() {
     
        let controller = webView.configuration.userContentController;
        controller.add(self, name: "userRiskResult")
        controller.add(self, name: "gotoBorrow")
        controller.add(self, name: "gotoShare")
        controller.add(self, name: "giveMeSomeString")
        
    }
    
    func removeAllScriptMsgHandle() {
        let controller = webView.configuration.userContentController;
controller.removeScriptMessageHandler(forName: "userRiskResult")
        controller.removeScriptMessageHandler(forName: "gotoBorrow")
        controller.removeScriptMessageHandler(forName: "gotoShare")
        controller.removeScriptMessageHandler(forName: "giveMeSomeString")

    }
    
    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        
        if message.name == "userRiskResult" {
            userRiskTestSuccess()
        }else if message.name == "userRiskResultLooser"{
            userRiskTestLooser()
        }else if message.name == "gotoBorrow"{
            
        }else if message.name == "gotoShare" {
            
        }else if message.name == "giveMeSomeString"{
            
        }
        
    }
}

extension GHBaseWebViewController{
    
    //风险评测成功
    func userRiskTestSuccess() {
        let riskTestResult = ViewController();
//        riskTestResult.style = .evaluateResutlSuccess
self.navigationController?.pushViewController(riskTestResult)
    }
    //风险评测失败
    func userRiskTestLooser() {
        let riskTestResultLosser = ViewController()
        self.navigationController?.pushViewController(riskTestResultLosser)
        
    }
}
